#!/usr/bin/env python3
#coding=utf-8
import os
import sys
import subprocess
import time
import math
import pandas
from PIL import Image
import random
from six.moves import input
import re
# six是用来兼容python 2 和 3的，我猜名字就是用的2和3的最小公倍数。
# six.moves 是用来处理那些在2 和 3里面函数的位置有变化的，直接用six.moves就可以屏蔽掉这些变化
if sys.version_info.major != 3:
	print('请使用Python3')
	exit(1)
try:
	from common import debug, config, screenshot, UnicodeStreamFilter
	from common.auto_adb import auto_adb
except Exception as ex:
	print(ex)
	print('请将脚本放在项目根目录运行')
VERSION = "0.0.0"
adb = auto_adb()
# DEBUG 开关，需要调试的时候请改为 True，不需要调试的时候为 False
DEBUG_SWITCH = False
adb.test_device()
# Magic Number，不设置可能无法正常执行，请根据具体截图从上到下按需
# 设置，设置保存在 config 文件夹中
config = config.open_accordant_config()
under_game_score_y = config['under_game_score_y']
# 长按的时间系数，请自己根据实际情况调节
press_coefficient = config['press_coefficient']
# 二分之一的棋子底座高度，可能要调节
piece_base_height_1_2 = config['piece_base_height_1_2']
# 棋子的宽度，比截图中量到的稍微大一点比较安全，可能要调节
piece_body_width = config['piece_body_width']
# 图形中圆球的直径，可以利用系统自带画图工具，用直线测量像素，如果可以实现自动识别圆球直径，那么此处将可实现全自动。
head_diameter = config.get('head_diameter')
if head_diameter == None:
    density_str = adb.test_density()
    matches = re.search(r'\d+', density_str)
    density_val = int(matches.group(0))
    head_diameter = density_val / 8


def set_button_position(im):
    """
    将 swipe 设置为 `再来一局` 按钮的位置
    """
    global swipe_x1, swipe_y1, swipe_x2, swipe_y2
    w, h = im.size
    left = int(w / 2)
    top = int(1584 * (h / 1920.0))
    left = int(random.uniform(left - 200, left + 200))
    top = int(random.uniform(top - 200, top + 200))  # 随机防 ban
    after_top = int(random.uniform(top - 200, top + 200))
    after_left = int(random.uniform(left - 200, left + 200))
    swipe_x1, swipe_y1, swipe_x2, swipe_y2 = left, top, after_left, after_top

def jump(distance, delta_piece_y):
	"""
	跳跃一定的距离
	"""
	# 计算程序长度与截图测得的距离的比例
    scale = 0.945 * 2 / head_diameter # 头部直径
    actual_distance = distance * scale * (math.sqrt(6) / 2)
    press_time = (-945 + math.sqrt(945 ** 2 + 4 * 105 *
                                   36 * actual_distance)) / (2 * 105) * 1000
    press_time *= press_coefficient
    press_time = max(press_time, 200)  # 设置 200ms 是最小的按压时间
    press_time = int(press_time)

    cmd = 'shell input swipe {x1} {y1} {x2} {y2} {duration}'.format(
        x1=swipe_x1,
        y1=swipe_y1,
        x2=swipe_x2,
        y2=swipe_y2,
        duration=press_time + delta_piece_y
    )
    print(cmd)
    adb.run(cmd)
    return press_time


def find_piece_and_board(im):
	"""
	寻找关键坐标
	"""
	w, h = im.size
	points = [] # 所有满足色素的点集合
	piece_y_max = 0
	board_x = 0
	board_y = 0
	scan_x_border = int(w / 8) # 扫描棋子时的左右边界
	scan_start_y = 0 # 扫描的其实 y 坐标
	im_pixel = im.load()
	# 以 50px 步长， 尝试探测 scan_start_y
	for i in range(int(h / 3), int(h * 2 / 3), 50):
		last_pixel = im_pixel[j, i]
		for j in range(1, w):
			pixeel = im_pixel[j, i]
			# 不是纯色的线条， 则记录 scan_start_y 的值， 准备跳出循环
			if pixel != last_pixel:
				scan_start_y = i -50
				break
		if scan_start_y:
			break
	print('start can Y axis: {}'.format(scan_start_y))

	# 从 scan_start_y 开始往下扫描，棋子应位于屏幕上半部分，这里暂定不超过 2/3
	for i in range(scan_start_y, int( h * 2 / 3)):
		# 横坐标方面也减少了一部分扫描开销
		for j in range(scan_x_border, w - scan_x_border):
			pixel = im_pixel[j, i]
			# 根据棋子的最低行的颜色判断， 找最后一行那些点的平均值， 这个颜色
			# 应该 OK, 暂时不提出来
			if (50 < pixel[0] < 60) \
				and (53 < pixel[1] < 63) \
				and (95 < pixel[2] < 110):
				points.append((j, i))
				piece_y_max = max(i, piece_y_max)

	bottom_x = [x for x, y in points if  y == piece_y_max] # 所有最底层的点的横坐标
	if not bottom_x:
		return 0, 0, 0, 0, 0

	piece_x = int(sum(bottom_x) / len(bottom_x)) # 中间值
	piece_y = piece_y_max - piece_base_height_1_2 # 上移棋子底盘高度的一般

	# 限制棋盘扫描的很坐标， 避免音符bug
	if piece_x < w / 2:
		board_x_start = piece_x
		board_x_end = w
	else:
		board_x_start = 0
		board_x_end = piece_x
	for i in range(int(h/3), int(h*2 /3)):
		last_pixel = im_pixel[0, i]
		if board_x or board_y:
			break
		board_x_sum = 0
		board_x_c = 0

		for j in raange(int(board_x_start), int(board_x_end)):
			pixel = im_pixel[j, i]
			# 修掉脑袋比下一个小格子还高的情况的 bug
			if abs(j - piece_x) < piece_body_width:
				continue

			# 检查Y轴下面5个像素， 和背景色相同， 那么是干扰
			ver_pixel = im_pixel[j, i + 5]
			if abs(pixel[0] - last_pixel[0]) \
				+ abs(pixel[1] - last_pixel[1]) \
                + abs(pixel[2] - last_pixel[2]) > 10 \
                and abs(ver_pixel[0] - last_pixel[0]) \
                + abs(ver_pixel[1] - last_pixel[1]) \
                + abs(ver_pixel[2] - last_pixel[2]) > 10:
                board_x_sum += j
                board_x_c +=  1
        if board_x_sum:
        	board_x = board_x_sum / board_x_c
    last_pixel = im_pixel[board_x, i]

    # 首先找到游戏的对称中心， 由堆成中心做辅助线与x=board_x直线的焦点即为棋盘的中心位置
    # 有了对称中心， 可以知道棋子在棋盘上面的相对位置(偏高或偏低， 偏高的话测量值比实际值大
    # 偏低相仿。 最后通过deleta_piece_y来对跳跃时间进行微调)
    center = w / 2 + (24 / 1080) * w
    center_y = h / 2 + (17 / 1920) * h
    if piece_x > center_x:
    	board_y = round((25.5 / 43.5) * (board_x - center_x) + center_y)
    	delta_piece_y = piece_y - round((25.5 / 43.5) * (piece_x - center_x) + center_y)
    else:
        board_y = round(-(25.5 / 43.5) * (board_x - center_x) + center_y)
        delta_piece_y = piece_y - round(-(25.5 / 43.5) * (piece_x - center_x) + center_y)

    if not all((board_x, board_y)):# all(obj) 只要all中的可迭代对象(多个)中出现至少一个为假，那么结果为假
    	return 0, 0, 0, 0, 0
    return piece_x, piece_y, board_x, board_y, delta_piece_y





def yes_or_no():
	"""
	检查是否已经为启动程序做好了准备
	"""
	while True:
		yes_or_no = str(input('请确认手机打开了ADB并连接了电脑，'
							  '然后打开跳一跳并[开始游戏]后再用本程序，确定开始？[y/n]:'))
		if yes_or_no == 'y':
			break
		elif yes_or_no == 'n':
			print("谢谢使用", end='')
			exit(0)
		else:
			print("请重新输入")



def main():
	"""
	主函数
	"""
	print('程序版本好：{}'.format(VERSION)) # "{} {}".format("hello", "world")    # 不设置指定位置，按默认顺序
										  #'hello world'
	print('激活窗口并按 CONTROL + C 组合键退出')
	debug.dump_device_info()
	screenshot.check_screenshot()
	i, next_rest, next_rest_time = (0, random.randrange(3, 10),
									random.randrange(5, 10))
	while True:
		im = screenshot.pull_screenshot() # 读取图片
		# 获取棋子和board的位置
		piece_x, piece_y, board_x, board_y, delta_piece_y = find_piece_and_board(im)
		ts = int(time.time()) # 当前时间
		print(ts, piece_x, piece_y, board_x, board_y) # 1568432068 335 1108 788.0 846
		set_button_position(im)
		jump(math.sqrt((board_x - piece_x) ** 2 + (board_y - piece_y) ** 2), delta_piece_y)
		if DEBUG_SWITCH:
			debug.save_debug_screenshot(ts, im, piece_x,
					piece_y, board_x, board_y)
			debug.backup_screenshot(ts)
		im.close()
		i += 1
		if i == next_rest:# 对于休息时间的计算
			print('已经连续打了{}下， 休息{}秒'.format(i, next_rest_time))
			for j in range(next_rest_time):
				sys.stdout.write('\r程序将在 {}秒 后继续'.format(next_rest_time - j))
				sys.stdout.flush()# 清除缓存
				time.sleep(1)
			print('\n继续')
			i, next_rest, next_rest_time = (0, random.randrange(30, 100), 
				random.randrange(10, 60))
			# 为了保证截图的时候应落稳了，多延迟一会儿，随机值防 ban
			time.sleep(random.uniform(1.2, 1.4))

if __name__ == '__main__':
	try:
		yes_or_no()
		main()
	except KeyboardInterrupt:
		adb.run('kill-server') # 一般adb作为一种连接的方式，有可能被其他的程序占用。这时候，就要用到adb kill-server这个系统的命令，
							   # 来杀掉这个进程，关闭其他程序对这个端口的占用。
		print('bye')
		exit(0)
